﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cofe.Core.Property;
using Cofe.Core.Security;
using Cofe.Core.Utils;
using Cofe.Core.Validater;

namespace Cofe.Core
{
    public static class IPropertyAttributeHelperExtension
    {
        public static PermissionType GetRequirePermissionForRead(this IPropertyAttributeHelper helper, object property)
        {
            var pt = helper.FindAttribute<RequirePermissionAttribute>(property);
            if (pt == null)
                return PermissionType.Read;
            else return pt.PermissionTypeForRead;
        }

        public static PermissionType GetRequirePermissionForWrite(this IPropertyAttributeHelper helper, object property)
        {
            var pt = helper.FindAttribute<RequirePermissionAttribute>(property);
            if (pt == null)
                return PermissionType.Write;
            else return pt.PermissionTypeForWrite;
        }

        /// <summary>
        /// Whether a property is tagged with MetadataAttribute.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public static bool IsWebMetadata(this IPropertyAttributeHelper helper, object property)
        {
            var prop = helper.FindAttribute<WebMetadataAttribute>(property);
            return prop is WebMetadataAttribute;
        }

        public static bool IsWebProperty(this IPropertyAttributeHelper helper, object property)
        {
            var prop = helper.FindAttribute<WebPropertyAttribute>(property);
            return prop is WebPropertyAttribute;
        }
        

        public static bool IsWebResource<T>(this IPropertyAttributeHelper helper, T property)
        {
            var prop = helper.FindAttribute<WebResourceAttribute>(property);
            return prop is WebResourceAttribute && !(prop is WebActionAttribute);
        }

        public static bool IsWebAction<T>(this IPropertyAttributeHelper helper, T property)
        {
            var prop = helper.FindAttribute<WebActionAttribute>(property);
            return prop is WebResourceAttribute;
        }

        public static bool IsAction<T>(this IPropertyAttributeHelper helper, T property)
        {
            return helper.FindAttribute<ActionAttribute>(property) != null;
        }

        public static bool IsAction<T>(this IPropertyAttributeHelper helper, T property, out ActionAttribute actionAttribute)
        {
            actionAttribute = helper.FindAttribute<ActionAttribute>(property);
            return actionAttribute != null;
        }

        public static bool IsCachable(this IPropertyAttributeHelper helper, object property)
        {
            return helper.GetPropertyCacheOption(property).HasFlag(PropertyCacheOptions.Cachable);
        }

        public static bool IsResettable(this IPropertyAttributeHelper helper, object property)
        {
            return helper.GetPropertyCacheOption(property).HasFlag(PropertyCacheOptions.Resettable);
        }

        public static bool IsChangable(this IPropertyAttributeHelper helper, object property)
        {
            return helper.GetPropertyCacheOption(property).HasFlag(PropertyCacheOptions.Changeable);
        }

        public static bool HasDefaultValue(this IPropertyAttributeHelper helper, string propertyString)
        {
            object property = helper.PropertyDictionary.GetProperty(propertyString);
            if (property != null)
                return helper.HasDefaultValue(property);
            else throw new KeyNotFoundException(propertyString);
        }

        public static object GetDefaultValue(this IPropertyAttributeHelper helper, string propertyString)
        {
            object property = helper.PropertyDictionary.GetProperty(propertyString);
            if (property != null)
                return helper.GetDefaultValue(property);
            else throw new KeyNotFoundException(propertyString);
        }

        public static string GetMIMEType(this IPropertyAttributeHelper helper, object property)
        {
            var wra = helper.FindAttribute<WebResourceAttribute>(property);
            if (wra == null)
                return "";
            else return DecoratorExtension.GetMIMEType(wra.MediaType);
        }

    }
}
